#encoding=utf-8
from utils.ext import inner_db
from marshmallow import Schema, fields

#被隔离的GPU
class Node_Attribute(inner_db.Model):
    __bind_key__ = 'platform'
    __tablename__ = 'node_attribute'

    node_id = inner_db.Column(inner_db.String(40), primary_key=True)
    unhealthy_gpu = inner_db.Column(inner_db.BigInteger)

    def get_data_info(self):
        return dict(node_id = self.node_id, unhealthy_gpu = self.unhealthy_gpu)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())

#隔离操作日志
class Resource_Action_Log(inner_db.Model):
    __bind_key__ = 'platform'
    __tablename__ = 'resource_action_log'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True)
    node_id = inner_db.Column(inner_db.String(40))
    value_before = inner_db.Column(inner_db.BigInteger)
    value_after = inner_db.Column(inner_db.BigInteger)
    action = inner_db.Column(inner_db.String(10))
    action_time = inner_db.Column(inner_db.DateTime, default =inner_db.func.now())
    resource_type = inner_db.Column(inner_db.String(10), default='gpu')
    operator = inner_db.Column(inner_db.String(20))
    remark = inner_db.Column(inner_db.String(1000))

    def get_data_info(self):
        return dict(id = self.id, node_id = self.node_id, value_before = self.value_before,
                    value_after = self.value_after, action = self.action, action_time = self.action_time,
                    resource_type = self.resource_type, operator = self.operator, remark = self.remark)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())

class ResourceLogQuerySchema(Schema):
    node_id = fields.String(required=True)

class ResourceAddSchema(Schema):
    node_id = fields.String(required=True)
    unhealthy_gpu = fields.Integer(required=True)
    remark = fields.String(required=True)

class ResourceDeleteSchema(Schema):
    node_id = fields.String(required=True)
